package com.study.thread;

/**
 * @author geticsen
 */
public class ThreadWaitAndNotifyObjLock {
    private static final Object obj = new Object();

    static class ThreadA implements Runnable {
        @Override
        public void run() {
            synchronized (obj) {
                for (int i = 0; i < 5; i++) {

                    try {
                        System.out.println("ThreadA i1 = " + i);
                        obj.notify();
                        obj.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                obj.notify();
            }

        }
    }

    static class ThreadB implements Runnable {
        @Override
        public void run() {
            synchronized (obj) {
                for (int i = 0; i < 5; i++) {
                    try {
                        System.out.println("ThreadB i1 = " + i);
                        obj.notify();
                        obj.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                obj.notify();
            }
        }
    }

    public static void main(String[] args) {
        new Thread(new ThreadA()).start();
        new Thread(new ThreadB()).start();

    }
}
